package com.smarthome.core.propertystaffmanager.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShPropertyCompanyService;
import com.smarthome.base.service.ShPropertyEmployeeService;
import com.smarthome.base.service.ShResidenceService;
import com.smarthome.core.propertystaffmanager.PropertyCompanyService;
import com.smarthome.domain.bean.ShPropertyCompany;
import com.smarthome.domain.bean.ShPropertyEmployee;
import com.smarthome.domain.bean.ShResidence;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.domain.vo.boss.Tree;

/**
 * @title 物业公司服务类
 * @author dhc
 * 
 */
@Service("propertyCompanyService")
public class PropertyCompanyServiceImpl implements PropertyCompanyService {

	@Resource(name = "shPropertyCompanyService")
	private ShPropertyCompanyService propertyCompanyService;

	@Resource(name="shPropertyEmployeeService")
	private ShPropertyEmployeeService employeeService;
	
	@Resource(name="shResidenceService")
	private ShResidenceService residenceService;
	@Override
	public ShPropertyCompany queryShPropertyCompanyById(Long id) {
		
		
		return propertyCompanyService.selectEntryById(id);
	}

	@Override
	public ShPropertyCompany queryShPropertyCompanyByEntry(
			ShPropertyCompany company) {

		if(company == null || company.getId() == null){
			return null;
		}
		
		return propertyCompanyService.selectEntryById(company.getId());
	}

	@Override
	public DataGrid qeuryDataGrid(ShPropertyCompany company, PageHelper page) {
		
		return propertyCompanyService.quetyDataGrid(company, page);
	}

	@Override
	public int add(ShPropertyCompany company) {
		
		
		return propertyCompanyService.saveOrUpdate(company);
	}

	@Override
	public int edit(ShPropertyCompany company) {
		
		ShPropertyCompany saveCompany = propertyCompanyService.selectEntryById(company.getId());
		
		if(null != saveCompany){
			BeanUtils.copyProperties(company, saveCompany);
			return propertyCompanyService.updateById(saveCompany);
		}
		return 0;
	}

	@Override
	public int delete(Long id) {
		ShResidence residence  = new ShResidence();
		residence.setPropertyId(id);
		List<ShResidence> rs =residenceService.selectEntryList(residence);
		if(null != rs && rs.size() > 0){
			throw new IllegalAccessError("房屋关联物业公司");
		} 
		
		ShPropertyEmployee employee = new ShPropertyEmployee();
		employee.setCompanyId(id);
		List<ShPropertyEmployee> cs = employeeService.selectEntryList(employee);
		if(null != cs && cs.size() > 0){
			throw new IllegalAccessError("物业人员关联物业公司");
		}
		return propertyCompanyService.deleteByIds(id);
	}

	@Override
	public int butchDelete(Long... id) {
		return propertyCompanyService.deleteByIds(id);
	}


	@Override
	public List<ShPropertyCompany> queryListByCompany(ShPropertyCompany company) {
		return propertyCompanyService.selectEntryList(company);
	}

	@Override
	public List<Combobox> getComboboxs(ShPropertyCompany company) {
		List<Combobox> comboboxs = new ArrayList<Combobox>();
		List<ShPropertyCompany>companys =  propertyCompanyService.selectEntryList(company);
		if(companys != null){
			for(ShPropertyCompany c : companys){
				comboboxs.add(new Combobox(c.getId()+"",c.getName()));
			}
		}
		return comboboxs;
	}

	@Override
	public List<Tree> queryTreeUser(ShPropertyCompany company) {
		List<ShPropertyCompany> rootList = propertyCompanyService.selectEntryList(company);
		List<Tree> roots = new ArrayList<Tree>();
		if(null == rootList || rootList.size() == 0){
			return roots;
		}
		
		Map<Long,List<Tree>> map = new HashMap<Long, List<Tree>>();
		for(ShPropertyCompany c : rootList){
			roots.add(new Tree(c.getId(), c.getName()));
			map.put(c.getId(), new ArrayList<Tree>());
		}
		
		List<ShPropertyEmployee> shs = employeeService.selectEntryList(null);

		if(null == shs){
			return roots;
		}
		
		for(ShPropertyEmployee e : shs){
			List<Tree> t = map.get(e.getCompanyId());
			if(null == t) continue;
			Tree tree  = new Tree();
			tree.setId(e.getId());
			tree.setPid(e.getCompanyId());
			tree.setText(e.getName());
			//tree.setAttributes(e);
			t.add(tree);
		}
		
		
		for(Tree t : roots){
			t.setChildren(map.get(t.getId()));
		}
		
		return roots;
	}

}
